package com.arvin.controller;

import com.arvin.pojo.Exempt;
import com.arvin.pojo.Log;
import com.arvin.pojo.PageBean;
import com.arvin.pojo.Result;
import com.arvin.service.ExemptService;
import com.arvin.service.LogService;
import com.arvin.utils.ThreadLocalUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/exem")
@Tag(name = "免试相关接口",description = "免试相关接口")
@Slf4j
public class ExemptController {

    @Autowired
    private ExemptService exemptService;
    @Autowired
    private LogService logService;

    /**
     * 修改免试申请状态
     */
    @Operation(summary = "修改免试申请状态",description = "修改免试申请状态")
    @PutMapping("/updateExemState")
    public Result updateExemState(@RequestBody Exempt exempt){
        //通过就exemState设为“已通过”，打回就设为“未通过”
        exemptService.updateExemState(exempt.getExemId(),exempt.getExemState());

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("免试");
        log.setLogOperate("管理员" + username + "将学号为" + exempt.getStudentId()
        + "的学生免试状态改为了" + exempt.getExemState());
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 获取免试人员名单
     * @param page
     * @param pageSize
     * @param studentId
     * @param stuName
     * @param className
     * @param exemDate
     * @param exemState
     * @return
     */
    @Operation(summary = "获取免试人员名单",description = "获取免试人员名单")
    @GetMapping("/getExemInfo")
    public Result<PageBean> getExemInfo(Integer page, Integer pageSize, String studentId, String stuName,
                                        String className, LocalDate exemDate, String exemState,Integer exemScore){
        PageBean pageBean = exemptService.getExemInfo(page, pageSize, studentId, stuName,className, exemDate, exemState,exemScore);
        log.info("免试学生信息{}", pageBean);
        return Result.success(pageBean);
    }

    /**
     * 设置免试通过后的面试分数
     * @param exempt
     * @return
     */
    @Operation(summary = "设置免试通过后的面试分数",description = "设置免试通过后的面试分数")
    @PostMapping("/setExemScore")
    public Result setExemScore(@RequestBody Exempt exempt){
        exemptService.setExemScore(exempt);

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("考试");
        log.setLogOperate("管理员" + username + "为学号为" + exempt.getStudentId()
                + "的学生进行免试中面试打分:" + exempt.getExemScore());
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 学生免试中面试批量合格
     * @param ids
     * @return
     */
    @Operation(summary = "学生免试中面试批量合格",description = "学生免试中面试批量合格")
    @PutMapping("/passByExemIds/{ids}")
    public Result passByIntvIds(@PathVariable List<Integer> ids) {
        exemptService.passByExemIds(ids,"合格");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("免试");
        log.setLogOperate("管理员" + username + "将免试id为" + ids
                + "的免试状态修改为：" + "合格");
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }

    /**
     * 学生免试中面试批量不合格
     * @param ids
     * @return
     */
    @Operation(summary = "学生免试中面试批量不合格",description = "学生免试中面试批量不合格")
    @PutMapping("/noPassByExemIds/{ids}")
    public Result noPassByIntvIds(@PathVariable List<Integer> ids) {
        exemptService.passByExemIds(ids,"不合格");

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        // 添加日志
        Log log = new Log();
        log.setUsername(username);
        log.setLogType("免试");
        log.setLogOperate("管理员" + username + "将免试id为" + ids
                + "的免试状态修改为：" + "不合格");
        log.setLogTime(LocalDateTime.now());
        logService.insert(log);

        return Result.success();
    }
}
